-
Notifications
You must be signed in to change notification settings - Fork 4
Update __init__.py to handle short report lengths. #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Added a check to readone that prevents a crash if the returned report is shorter than expected
can you try this version? diff --git a/src/radexreader/__init__.py b/src/radexreader/__init__.py
index 4238f6c238..6a42905cb2 100644
--- a/src/radexreader/__init__.py
+++ b/src/radexreader/__init__.py
@@ -278,30 +278,32 @@
self.hid_set_report((0x7b, 0xff, 0x20, 0, 0x06, 0, self.keyA, self.keyB, 0, 0, self.keyC, self.keyD, 0, 0x08, 0x0c, 0, 0xf3, 0xf7))
hexa = self.hid_get_report()
+ if hexa != b'' and len(hexa) >= 31:
+ # measure = 0.15 µSv/h = 15 / 0.15 µSv accumulated = 15 / 15 CPM = 15
+ measure = (hexa[20] + hexa[21] * 256 + hexa[22] * 256 * 256) / 100
+ measure_acc = (hexa[24] + hexa[25] * 256 + hexa[26] * 256 * 256) / 100
+ measure_cpm = hexa[28] + hexa[29] * 256 + hexa[30] * 256 * 256
+ # uncertainty of the result
+ percent = 15 + 6 / measure
+ measure_min = measure * (1 - percent / 100)
+ measure_max = measure * (1 + percent / 100)
+ if measure_min < 0:
+ measure_min = 0
+ if percent > 99.9:
+ percent = 99.9
+ # most recent measure
+ timestamp = int(time.time())
+ return { timestamp: {
+ 'pct': percent,
+ 'min': measure_min,
+ 'val': measure,
+ 'max': measure_max,
+ 'acc': measure_acc,
+ 'cpm': measure_cpm,
+ 'time': timestamp
+ } }
- # measure = 0.15 µSv/h = 15 / 0.15 µSv accumulated = 15 / 15 CPM = 15
- measure = (hexa[20] + hexa[21] * 256 + hexa[22] * 256 * 256) / 100
- measure_acc = (hexa[24] + hexa[25] * 256 + hexa[26] * 256 * 256) / 100
- measure_cpm = hexa[28] + hexa[29] * 256 + hexa[30] * 256 * 256
- # uncertainty of the result
- percent = 15 + 6 / measure
- measure_min = measure * (1 - percent / 100)
- measure_max = measure * (1 + percent / 100)
- if measure_min < 0:
- measure_min = 0
- if percent > 99.9:
- percent = 99.9
-
- timestamp = int(time.time())
- return { timestamp: {
- 'pct': percent,
- 'min': measure_min,
- 'val': measure,
- 'max': measure_max,
- 'acc': measure_acc,
- 'cpm': measure_cpm,
- 'time': timestamp
- } }
+ return {}
def erase(self):
if self.com == 'RD1212v2':
it's similar between RD1212 and One, and without print |
It appears that applying that patch caused everything that's returned to be empty. When I reverted to the old code it started returning data again. I was testing why that was, but I didn't figure it out. |
loool, I suppose this line: |
Oh so nice! There is probably something wrong with the addresses used to read data from the Radex ONE.
But the address is not "simple": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a safeguard in readOne
to prevent crashes when the HID report is shorter than expected.
- Introduces a length check for the received report and early return on incomplete data
- Emits a warning and returns an empty dict when the report is too short
Comments suppressed due to low confidence (1)
src/radexreader/init.py:282
- Add unit tests covering the new branch where
hexa
isNone
or shorter than expected to validate this behavior and prevent regressions.
# Check if the returned report has the expected length
It look like that there are negative values with current code:
with ChatGPT I tried some stupid things, and there is still something wrong, because his helper made never breaks: import csv
# the correct initial values
keyA = 0x04 - 0x04 # => 0x00
keyB = 0x00
keyC = 0x5a + 0x04 # => 0x5e
keyD = 0x00
seen = set()
rows = []
iteration = 1
while iteration <= 10000:
t = (keyA, keyB, keyC, keyD)
if t in seen:
break
seen.add(t)
rows.append([iteration, f"0x{keyA:02x}", f"0x{keyB:02x}", f"0x{keyC:02x}", f"0x{keyD:02x}"])
iteration += 1
keyA += 0x04
keyC -= 0x04
if keyA > 0xff:
keyA -= 0xfe
keyB += 0x01
if keyB > 0xff:
keyB = 0x00
keyC -= 0x01
elif keyC < 0x00:
keyC += 0xff
keyD -= 0x01
if keyD < 0x00:
keyD = 0xff
with open("radex_keys_cycle.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter="\t")
writer.writerow(["iteration", "keyA", "keyB", "keyC", "keyD"])
writer.writerows(rows) |
It could be nice to know which address crash in your beautiful graph. I suppose that the beginning of the iteration is correct, we need to identify working addresses, and the first one that crash ( |
I added a check to readone that prevents a crash if the returned report is shorter than expected.
I encountered an issue with my code crashing due to some of the reports being shorter than expected, this stop-gap addition prevents the code from crashing out.